import logging
from datetime import timedelta

from dateutil.relativedelta import relativedelta

import odoo
from odoo import api, fields, models

_logger = logging.getLogger(__name__)

BASE_VERSION = odoo.modules.load_information_from_description_file('base')['version']
MAX_FAIL_TIME = timedelta(hours=5)

class BadVersion(Exception):
    pass

class BadModuleState(Exception):
    pass


_intervalTypes = {
    'days': lambda interval: relativedelta(days=interval),
    'hours': lambda interval: relativedelta(hours=interval),
    'weeks': lambda interval: relativedelta(days=7*interval),
    'months': lambda interval: relativedelta(months=interval),
    'minutes': lambda interval: relativedelta(minutes=interval),
}


class ir_cron(models.Model):
    _name = "ir.cron"
    _order = 'cron_name'
    _description = 'Scheduled Actions'
    ir_actions_server_id = fields.Many2one(
        'ir.actions.server', 'Server action',
        delegate=True, ondelete='restrict', required=True)
    cron_name = fields.Char('Name', related='ir_actions_server_id.name', store=True, readonly=False)
    user_id = fields.Many2one('res.users', string='Scheduler User', default=lambda self: self.env.user,required=True)
    active = fields.Boolean(default=True)
    interval_number = fields.Integer(default=1, help="Repeat every x.")
    interval_type = fields.Selection([('minutes', 'Minutes'),
                                      ('hours', 'Hours'),
                                      ('days', 'Days'),
                                      ('weeks', 'Weeks'),
                                      ('months', 'Months')], string='Interval Unit', default='months')
    numbercall = fields.Integer(string='Number of Calls', default=1, help='How many times the method is called,\na negative number indicates no limit.')
    doall = fields.Boolean(string='Repeat Missed', help="Specify if missed occurrences should be executed when the server restarts.")
    nextcall = fields.Datetime(string='Next Execution Date', required=True, default=fields.Datetime.now, help="Next planned execution date for this job.")
    priority = fields.Integer(default=5, help='The priority of the job, as an integer: 0 means higher priority, 10 means lower priority.')

    @api.model
    def create(self, values):
        @api.model
        def create(self, values):
            values['usage'] = 'ir_cron'
            return super(ir_cron, self).create(values)

    @api.model
    def default_get(self, fields_list):
        pass

    @api.multi
    def method_direct_trigger(self):
        pass

    @api.model
    def _handle_callback_exception(self, cron_name, server_action_id, job_id, job_exception):
        pass

    @api.model
    def _callback(self, cron_name, server_action_id, job_id):
        pass

    @classmethod
    def _process_job(cls, job_cr, job, cron_cr):
        pass

    @classmethod
    def _process_jobs(cls, db_name):
        pass

    @classmethod
    def _acquire_job(cls, db_name):
        pass

    @api.multi
    def _try_lock(self):
        pass

    @api.multi
    def write(self, vals):
        pass

    @api.multi
    def unlink(self):
        pass

    @api.multi
    def try_write(self, values):
        pass

    @api.model
    def toggle(self, model, domain):
        pass

